package com.arvato.minipro.service.mall;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.arvato.minipro.bean.bean_do.mall.CommodityCategoryDo;
import com.arvato.minipro.bean.bean_do.mall.EcCommodityDo;
import com.arvato.minipro.bean.bean_do.mall.CommodityEffectDo;
import com.arvato.minipro.bean.bean_do.mall.CommodityImgDo;
import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_vo.mall.EcCategoryVo;
import com.arvato.minipro.bean.bean_vo.mall.CommodityCutVo;
import com.arvato.minipro.bean.bean_vo.mall.CommodityImgVo;
import com.arvato.minipro.bean.bean_vo.mall.EcCommodityVo;
import com.arvato.common.ftp.FtpTool;
import com.arvato.common.util.UUIDUtil;
import com.arvato.minipro.mapper.mall.EcCommodityMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * Describe：商品业务service
 * 
 * @author smallking
 *
 *  2018年2月8日
 */
@Service
@Transactional
public class CommodityService {

	@Autowired
	private FtpTool ftpTool;

	@Autowired
	private EcCommodityMapper commodityMapper;
	
	@Autowired
	private CommodityImgService commodityImgService;
	
	@Autowired
	private CategoryService categoryService;
	
	@Autowired
	private CommodityCategoryService commodityCategoryService;

	@Autowired
	private CommodityEffectService commodityEffectService;
	
	@Autowired
	private CommodityBillboardService commodityBillboardService;
	
	@Autowired
	private CommodityCutService commodityCutService;

	/**
	 * 根据ID查询商品
	 * @param commodityId
	 * @return
	 */
	public EcCommodityVo getCommodityById(String commodityId) {
		EcCommodityVo commodityVo = commodityMapper.getCommodityById(commodityId);
		setCommodity(commodityVo);
		return commodityVo; 
	}
	
	/**
	 * 按条件查询商品(含分页)
	 * @param query
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	
	public PageData listCommodityByQuery(Query query, Integer pageNum, Integer pageSize) {
		// 设置分页参数
		PageHelper.startPage(pageNum, pageSize);
		// 查询数据
		List<EcCommodityVo> commodityList = commodityMapper.listCommodityByQuery(query);
		// 得到分页结果
		PageInfo<EcCommodityVo> pageInfo = new PageInfo<EcCommodityVo>(commodityList);
		List<EcCommodityVo> resultList = pageInfo.getList();
		for(EcCommodityVo commodityVo : resultList){
			setCommodity(commodityVo);
		}
		PageData pageData = new PageData(pageInfo.getTotal(), resultList);
		return pageData;
	}
	/**
	 * 按条件查询商品(不分页)
	 * @param query
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<EcCommodityVo> listCommodityByQuery(Query query) {
		List<EcCommodityVo> commodityList = commodityMapper.listCommodityByQuery(query);
		return commodityList;
	}
	
	/**
	 * 新增一个商品
	 * @param commodityDo 
	 * @param imgIdList 商品图片的Id数组
	 * @param categoryId 商品类别的Id
	 * @param effectIdList 商品功能Id数组
	 * @return
	 */
	public int insert(EcCommodityDo commodityDo, String[] imgIdList, String categoryId, String[] effectIdList) {
		int row = commodityMapper.insert(commodityDo);
		if(row == 1){
			// 插入商品图片和商品类别
			if(imgIdList != null){
				for(String imgId : imgIdList){
					// 查出当前商品已经的图片 设置商品排序码
					Query query = new Query();
					query.put("commodityId", commodityDo.getCommodityId());
					List<CommodityImgVo> commodityImgList = commodityImgService.listCommodityImgByQuery(query);
					CommodityImgDo img = new CommodityImgDo();
					img.setCommodityId(commodityDo.getCommodityId());
					img.setImgId(imgId);
					img.setSortCode(commodityImgList.size() + 1);
					commodityImgService.update(img);
				}
			}
			// 插入商品类别
			if(categoryId != null){
				CommodityCategoryDo cc = new CommodityCategoryDo();
				cc.setCategoryId(categoryId);
				cc.setCommodityId(commodityDo.getCommodityId());
				commodityCategoryService.insert(cc);
			}
			// 插入商品功能
			if(effectIdList != null) {
				for(String effectId: effectIdList) {
					CommodityEffectDo ce = new CommodityEffectDo();
					ce.setEffectId(effectId);
					ce.setCommodityId(commodityDo.getCommodityId());
					commodityEffectService.insert(ce);
				}
			}
		}
		return row;
	}
	
	/**
	 * 修改商品
	 * @param commodityDo
	 * @param categoryId 
	 * @param effectList
	 * @return
	 */
	public int update(EcCommodityDo commodityDo, String categoryId, String[] effectList) {
		int row = commodityMapper.update(commodityDo);
		// 处理商品类别
		if(categoryId != null) {
			Query query = new Query();
			query.put("commodityId", commodityDo.getCommodityId());
			commodityCategoryService.delete(query);
			CommodityCategoryDo cc = new CommodityCategoryDo();
			cc.setCommodityId(commodityDo.getCommodityId());
			cc.setCategoryId(categoryId);
			commodityCategoryService.insert(cc);
		}
		// 处理商品功能
		if(effectList != null && effectList.length >= 1){
			// 先删除掉之前的商品和功能关联关系
			Query query = new Query();
			query.put("commodityId", commodityDo.getCommodityId());
			commodityEffectService.delete(query);
			// 重新配置商品的类别的关联关系
			CommodityEffectDo ce = new CommodityEffectDo();
			for(String effectId : effectList){
				ce.setEffectId(effectId);
				ce.setCommodityId(commodityDo.getCommodityId());
				ce.setCommodityEffectId(UUIDUtil.getUUID("ce"));
				commodityEffectService.insert(ce);
			}
		}
		return row;
	}
	/**
	 * 删除一个商品
	 * @param commodityId
	 * @return
	 */
	public int delete(String commodityId) {
		EcCommodityVo commodityVo = commodityMapper.getCommodityById(commodityId);
		int row = commodityMapper.delete(commodityId);
		if(row == 1){
			// 删除商品的详情图片
			String detailImgName = commodityVo.getDetailPath();
			ftpTool.delete(detailImgName); 
			// 如果删除成功 就同时删除商品的图片和与之关联的分类
			commodityImgService.deleteByCommodityId(commodityId);
			// 删除商品和分类之间的关联关系
			Query query = new Query();
			query.put("commodityId", commodityId);
			commodityCategoryService.delete(query);
			// 删除此商品和广告位的关联
			commodityBillboardService.delete(query);
		}
		return row;
	}
	/**
	 * 查询商品的图片，商品的类别并且赋值到商品对象上
	 * @param commodityVo
	 */
	public void setCommodity(EcCommodityVo commodityVo){
		// 设置commodityVo的图片 分类 等一些信息
		if(commodityVo == null) {
			return;
		}
		Query query = new Query();
		query.put("commodityId", commodityVo.getCommodityId());
		List<CommodityImgVo> imgList = commodityImgService.listCommodityImgByQuery(query);
		commodityVo.setCommodityImgList(imgList);
		// 查询商品的类别列表
		List<EcCategoryVo> categoryList = categoryService.listCategoryByQuery(query);		
		commodityVo.setCategoryVoList(categoryList);
		// 查询当前商品是否支持砍价
		CommodityCutVo commodityCutVo = commodityCutService.getCommodityCutByCommodityId(commodityVo.getCommodityId());
		if(commodityCutVo != null) {
			commodityVo.setCut(true);
			commodityVo.setCommodityCutId(commodityCutVo.getCommodityCutId());
		}
	}
}
